package com.xzgj.fmmall.controller;

import com.xzgj.fmmall.entity.Goods;
import com.xzgj.fmmall.entity.ShoppingCart;
import com.xzgj.fmmall.service.ProductService;
import com.xzgj.fmmall.service.ShoppingCartService;
import com.xzgj.fmmall.vo.ResStatus;
import com.xzgj.fmmall.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Api(value = "提供购物车业务的相关接口",tags = "购物车管理")

public class ShoppingCartController {

    @Resource
    private ProductService productService;
    @Resource
    private ShoppingCartService shoppingCartService;

    /**
     * 传统的url    http://localhost:8080/goods/delete?goodsId=1 int goodId
     * RWETful url  http://localhost:8080/goods/delete/1 @PathVariable("gid") int goodId
     */
    //规范 1 一个url 代表一个资源

    @ApiOperation("通过购物车记录id删除商品")
    @DeleteMapping(value = "/{gid}")
    public ResultVO deleteShoppingCarts(@PathVariable("gid") String goodIds){

        ResultVO resultVO = null;
        try {
            resultVO = shoppingCartService.deleteShoppingCartsByCartIds(goodIds);
        } catch (Exception e){
            e.printStackTrace();
            resultVO = new ResultVO(ResStatus.OK,ResStatus.operationFailure,null);
        }
        return resultVO;
    }

    @ApiOperation("查询所有购物车商品列表")
    @GetMapping(value = "/list")
    public ResultVO listGoods(){

        return productService.selectAllProducts();
    }

    @ApiOperation("添加购物车商品")
    @PostMapping(value = "/cart/add")
    public ResultVO addShoppingCart(@RequestBody ShoppingCart cart, @RequestHeader("token") String token){//

        return shoppingCartService.addShoppingCart(cart);
    }

    @ApiOperation("根据用户ID查询购物车商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int",name = "userId",value = "用户id",required = true)
    })
    @GetMapping(value = "/cart/list")
    public ResultVO cartList(Integer userId, @RequestHeader("token") String token){//

           ResultVO resultVO = shoppingCartService.listShoppingCartsByUserId(userId);
            return resultVO;
    }

    @ApiOperation("根据购物车cartId更新购物车记录信息")
    @PutMapping("/update/{cartId}/{cartNum}")
    public ResultVO updateCardNum(@PathVariable("cartId") Integer cartId,
                                  @PathVariable("cartNum") Integer cartNum,
                                  @RequestHeader("token") String token){//

        ResultVO resultVO = shoppingCartService.updateCartNum(cartId,cartNum);
        return resultVO;
    }

    @ApiOperation("根据用户购物车cart_id查询订单/购物车信息")
    @ApiImplicitParam(dataType = "string",name = "cartIds",value = "选择的购物车记录id",required = true)

    @GetMapping("/listByCartIds")
    public ResultVO listShoppingCartsByCartIds(String cartIds,
                                               @RequestHeader("token") String token){//


        ResultVO resultVO = shoppingCartService.listShoppingCartsByCartIds(cartIds);
        return resultVO;
    }
    //规范 2 使用不同的请求方式表示不同的操作

    /**
     * post 添加
     * get  查询
     * put  修改
     * delete 删除
     * option 预检
     * @param goodId
     * @return
     */

    @ApiOperation("查询(单个)商品")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ResponseBody
    public ResultVO getGoods(@PathVariable("id") int goodId){

        return new ResultVO(ResStatus.OK,"暂时没有用到",null);
    }
    //规范 3 接口响应的资源的表现形式采用json（或者xml）
    /**
     * 在控制器类或每个接口方法添加@ResponseBody/@RestController注解将返回的对象格式化为json
     */
    //规范 4 前端（Android、ios、pc）通过无状态的HTTP协议与后端接口进行交互
}
